package project3cs424.src;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;

public class MeteorScraper {
	
	
	public static void main(String args[])throws IOException
	{
		String inputUrl="http://www.amsmeteors.org/fireball2/public.php?start_date=2001-12-01&end_date=2010-12-31&state=&event_id=&submit=Find+Reports";
		
		URL url=new URL(inputUrl);
		
		InputStreamReader isr=new InputStreamReader(url.openStream());
		
		BufferedWriter outputWriter=new BufferedWriter(new FileWriter(new File("project3cs424/meteors")));
		
		int count=0;
		Tidy tidy=new Tidy();
		tidy.setQuiet(true);
		tidy.setShowWarnings(false);
		tidy.setForceOutput(true);
		
		Document document=tidy.parseDOM(isr, null);
		
		Node tableNode=document.getElementsByTagName("table").item(0);
				
		Node tbodyNode=tableNode.getChildNodes().item(1);
		NodeList tbodyChildren=tbodyNode.getChildNodes();
		for(int i=0;i<tbodyChildren.getLength();i++)
		{
			System.out.println(tbodyChildren.item(i).getNodeName());
			Node trNode=tbodyChildren.item(i);
			if(trNode.getNodeName().equals("tr"))
			{
				NamedNodeMap trAttributes=trNode.getAttributes();
				Node classAttribute=trAttributes.getNamedItem("class");
				if(classAttribute!=null)
				{
					count++;
					System.out.println("not null");
					NodeList trChildren=trNode.getChildNodes();
					Node meteorIdNode=trChildren.item(1);
					Node dateNode =trChildren.item(2);
					Node timeNode=trChildren.item(3);
					Node meteorName=trChildren.item(4);
					Node location=trChildren.item(5);
					outputWriter.write(getTextFromNode(meteorIdNode)+"\t"+getTextFromNode(dateNode)+"\t"+getTextFromNode(timeNode)+"\t"+getTextFromNode(meteorName)+"\t"+getTextFromNode(location)+"\n");
				}
				
			}
		}
		outputWriter.close();
		System.out.println(count);
	}
	
	
	public static String getTextFromNode(Node node)
	{
		String text="";
		if(node.hasChildNodes())
		{
			NodeList nodeChildren=node.getChildNodes();
			for(int i=0;i<nodeChildren.getLength();i++)
			{
				text+=getTextFromNode(nodeChildren.item(i));
			}
		}
		else if(node.getNodeName().equals("#text"))
		{
			text=node.getNodeValue();
		}
		return text;
	}
	

}
